package com.jett.JFtpServer;
import cn.hutool.core.io.FileUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.setting.dialect.Props;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.jett.JFtpServer.BO.FtpServerConfig;
import com.jett.JFtpServer.Enum.RetMsg;
import com.jett.JFtpServer.Filter.JFtpServerWriteLIstFilter;
import com.jett.JFtpServer.JFtpServer.JFtpServer;
import com.jett.JFtpServer.Utils.SM4Util;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ipfilter.SessionFilter;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
public class BootStrap {
    public static Properties properties = new Properties();
    private static final String FTP_CONFIG_PATH = "FtpConfigPath";
    private static final String SM4_KEY = "SM4Key";
    private static final Log log = LogFactory.get();
    private static final String ftpConfigPath = System.getProperty("FtpConfigPath");
    private static final String sm4Key = System.getProperty("SM4Key");
    private static final String ftpConfigPathStr;

    static {
        if (ftpConfigPath == null) {
            ftpConfigPathStr = "ftp.config";
        } else {
            ftpConfigPathStr = ftpConfigPath + "/ftp.config";
        }
    }
    public static void main(String[] args) {
        SM4Util sm4;
        if (sm4Key != null) {
            sm4 = new SM4Util(sm4Key);
        } else {
            sm4 = new SM4Util();
        }

        Props props = new Props(ftpConfigPathStr);
        FtpServerConfig ftpServerConfig = new FtpServerConfig();
        ftpServerConfig.setFtpServerPath(sm4.decryptStr(props.getProperty("ftpServerPath"), true, RetMsg.Err0001));
        ftpServerConfig.setUser(sm4.decryptStr(props.getProperty("user"), true, RetMsg.Err0003));
        ftpServerConfig.setPassword(sm4.decryptStr(props.getProperty("password"), true, RetMsg.Err0004));
        ftpServerConfig.setIpWriteList(sm4.decryptStr(props.getProperty("ipWriteList"), false, (RetMsg)null));
        log.info("FTP根路径:" + ftpServerConfig.getFtpServerPath());
        log.info("用户名:" + ftpServerConfig.getUser());
        log.info("密码 :" + ftpServerConfig.getPassword());
        log.info("IP白名单:" + ftpServerConfig.getIpWriteList());
        if (!FileUtil.exist(ftpServerConfig.getFtpServerPath())) {
            log.info("ftpServerPath路径未配置,自动创建");
            File mkdir = FileUtil.mkdir(ftpServerConfig.getFtpServerPath());
            if (mkdir == null) {
                log.error("创建目录" + ftpServerConfig.getFtpServerPath() + "失败!");
                throw new RuntimeException(RetMsg.Err0002.getRetMsg());
            }
        }

        List<Authority> authoritys = new ArrayList();
        authoritys.add(new WritePermission());
        BaseUser baseUser = new BaseUser();
        baseUser.setName(ftpServerConfig.getUser());
        baseUser.setPassword(ftpServerConfig.getPassword());
        baseUser.setAuthorities(authoritys);
        baseUser.setHomeDirectory(ftpServerConfig.getFtpServerPath());
        SessionFilter sessionFilter = new JFtpServerWriteLIstFilter(ftpServerConfig.getIpWriteList());
        JFtpServer.create().addUser(baseUser).setPort(26021).start(sessionFilter);
    }
}
